import java.util.Arrays;

public class FindMinArrowShots {

    public static void main(String[] args) {
        FindMinArrowShots findMinArrowShots = new FindMinArrowShots();
        int[][] nums = {{-2147483646,-2147483645},{2147483646,2147483647}};
        System.out.println(findMinArrowShots.findMinArrowShots(nums));

    }

    public int findMinArrowShots1(int[][] points) {
        if (points.length == 1) return 1;
        Arrays.sort(points, (a, b) -> {
            if (a[0] == b[0]) return Integer.compare(a[1], b[1]);
            return Integer.compare(a[0], b[0]);
        });
        int start;
        int end;
        int index = 0;
        int ans = 0;
        while (index < points.length - 1) {
            if (points[index + 1][0] > points[index][1]) {
                ++ans;
                ++index;
            } else {
                start = Math.max(points[index][0], points[index + 1][0]);
                end = Math.min(points[index][1], points[index + 1][1]);
                ++index;
                while (index + 1 < points.length
                        && !(points[index + 1][1] < start || points[index + 1][0] > end)) {
                    start = Math.max(start, points[index + 1][0]);
                    end = Math.min(end, points[index + 1][1]);
                    ++index;
                }
                ++index;
                ++ans;
            }
        }
        if (index == points.length - 1) ++ans;
        return ans;
    }

    public int findMinArrowShots(int[][] points) {
        Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
        int end = points[0][1];
        int ans = 1;
        for (int[] point : points) {
            if (point[0] > end) {
                ++ans;
                end = point[1];
            }
        }
        return ans;
    }
}
